package com.jcl.recruit.searchresume.mapper;

import com.jcl.recruit.searchresume.model.SearchResumeRecord;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface SearchResumeRecordMapper {

    @Select({
            "<script>",
            "SELECT * FROM search_resume_record where is_del = 0",
            "<if test=\"null != userId\"> AND user_id=#{userId} </if>",
            "<if test=\"null != keyword\"> AND keyword=#{keyword} </if>",
            "<if test=\"null != city\"> AND city=#{city} </if>",
            "<if test=\"null != education\"> AND education=#{education} </if>",
            "<if test=\"null != workYear\"> AND work_year=#{workYear} </if>",
            "<if test=\"null != wechatPush\"> AND wechat_push=#{wechatPush} </if>",
            "<if test=\"null != emailPush\"> AND email_push=#{emailPush}</if>",
            "</script>"
    })
    List<SearchResumeRecord> selectSearchResumeRecord(@Param("userId") Integer userId,
                                                      @Param("keyword") String keyword,
                                                      @Param("city") String city,
                                                      @Param("education") String education,
                                                      @Param("workYear") Integer workYear,
                                                      @Param("wechatPush") Integer wechatPush,
                                                      @Param("emailPush") Integer emailPush);

    @Select({
            "select * from search_resume_record where user_id = #{userId} and is_del = 0 order by update_time desc"
    })
    List<SearchResumeRecord> selectSearchResumeRecordByUserId(@Param("userId") Integer userId);


    @Select({
            "select * from search_resume_record where user_id = #{userId} and is_del = 0 and wechat_push = 1 order by update_time desc"
    })
    List<SearchResumeRecord> selectWechatPushRecordByUserId(@Param("userId") Integer userId);

    @Select({
            "select * from search_resume_record where id = #{id}"
    })
    SearchResumeRecord selectSearchResumeRecordById(@Param("id") Integer id);

    @InsertProvider(type= SearchResumeRecordSqlProvider.class, method="insertSelective")
    @Options(useGeneratedKeys=true,keyProperty="id")
    int insertSelective(SearchResumeRecord record);

    @UpdateProvider(type= SearchResumeRecordSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(SearchResumeRecord record);

    @Update({
            "update search_resume_record set is_del = 1 where id = #{id}"
    })
    int deleteSearchResumeRecordById(@Param("id") Integer id);

    @Select({
            "select DISTINCT keyword from search_resume_record where is_del = 0 and (crawing_from is null or crawing_from not like '%${from}%')",
            "order by id limit 1"
    })
    String selectKeywordByCrawing(@Param("from") String from);

    @Select({
            "select id,crawing_from from search_resume_record where keyword = #{keyword} and is_del = 0"
    })
    List<SearchResumeRecord> selectListsByKeyword(@Param("keyword") String keyword);

    @Update({
            "<script>",
            "<foreach collection='list' item='entity' index='index' separator=';'>",
            "update search_resume_record set crawing_from = #{entity.crawingFrom},update_time = #{entity.updateTime} where id = #{entity.id}",
            "</foreach>",
            "</script>"
    })
    int batchUpdate(@Param("list") List<SearchResumeRecord> list);

    @Select({
            "select * from search_resume_record where datediff(update_time,#{date})=0 and keyword = #{keyword} and is_del = 0 order by id limit 1"
    })
    SearchResumeRecord selectPrimaryKeyByKeywordAndUpdatime(@Param("date") String date, @Param("keyword") String keyword);

    @Update({
            "update search_resume_record set crawing_from = null,update_time = #{time} where is_del = 0"
    })
    int batchUpdateCrawing(@Param("time") Date time);
}
